package Io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class ThireFile
{
	public static void main(String[] args)
	{
		System.out.println("========");
		fileReader();
		fileWriter();
	}

	private static void fileWriter()
	{
		BufferedWriter fw = null;
		try
		{
			FileOutputStream rest = new FileOutputStream("D:/a/b.txt",true);
			OutputStreamWriter fgw = new OutputStreamWriter(rest,"GBK");
			fw = new BufferedWriter(fgw);
			fw.write("李善壮，李善庄");
			fw.flush();
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}finally 
		{
			try
			{
				if(fw != null)
				{
					fw.close();
					fw = null;
				}
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

	private static void fileReader()
	{
		BufferedReader fr = null;
		try
		{
			FileInputStream re = new FileInputStream("D:/a/a.txt");
			InputStreamReader fe = new InputStreamReader(re,"GBK");
			fr = new BufferedReader(fe);
			String rest = null;
			while((rest = fr.readLine()) != null) 
			{
				System.out.println(rest);
			}
			
		} catch (IOException e)
		{
			e.printStackTrace();
		}finally
		{
			try
			{
				if(fr != null)
				{
					fr.close();
				}
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}
}
